package org.aaron.demos.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class MyContentHandler extends DefaultHandler {
	private String tagname;
	private String attrname, attrvalue;

	@Override
	public void startDocument() throws SAXException {
		Log.d("MyContentHandler", "startDocument");
	}

	@Override
	public void endDocument() throws SAXException {
		Log.d("MyContentHandler", "endDocument");
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		tagname = localName;
		if ("string".equals(localName)) {
			for (int i = 0; i < attributes.getLength(); i++) {
				if ("name".equals(attributes.getLocalName(i))) {
					attrname = attributes.getValue(i);
				}
			}
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if ("string".equals(localName)) {
			println();
		}
	}

	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
		if ("string".equals(tagname)) {
			attrvalue=new String(ch,start,length);
		}
	}

	private void println() {
		Log.d("MyContentHandler Println", attrname + "=" + attrvalue);
	}
}
